package io.websocketdemo.my;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.ByteBuffer;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class MyWebsocketClient {

    final String TAG = "ucc";
    WebSocketClient client;

    public MyWebsocketClient() {

    }

    public void Connect() {

        URI serverURI = URI.create("wss://192.168.6.80:7001/ws/push/one");

        client = new WebSocketClient(serverURI) {
            @Override
            public void onOpen(ServerHandshake handshakeData) {
                Log.i(TAG, "onOpen");
            }

            @Override
            public void onMessage(String message) {
                onMyMessage(message);
            }

            @Override
            public void onMessage(ByteBuffer bytes) {
                onMyMessage(bytes);
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                Log.i(TAG, String.format("onClose %d %s %b", code, reason, remote));
            }

            @Override
            public void onError(Exception ex) {
                Log.i(TAG, "onError" + ex.getMessage());
            }
        };

        Log.i(TAG, "connect==================");
        trustAllHosts(client);

        client.connect();
    }

    public void send(String msg) {
        if (this.client != null && this.client.isOpen()) {
            this.client.send(msg);
        }
    }

    public void send(ByteBuffer bytes) {
        if (this.client != null && this.client.isOpen()) {
            this.client.send(bytes);
        }
    }

    public void send(byte[] bytes) {
        if (this.client != null && this.client.isOpen()) {
            this.client.send(bytes);
        }
    }


    public void onMyMessage(String message) {
        Log.i(TAG, "onMessage");
    }

    public void onMyMessage(ByteBuffer bytes) {
        Log.i(TAG, "onMessage");
    }

    /**
     * 忽略证书
     *
     * @paramclient
     */
    void trustAllHosts(WebSocketClient client) {

        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }};

        try {
            SSLContext ssl = SSLContext.getInstance("SSL");
            ssl.init(null, trustAllCerts, new java.security.SecureRandom());

            SSLSocketFactory socketFactory = ssl.getSocketFactory();
            client.setSocketFactory(socketFactory);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
